<?php
if (!CUSTOM_TAGS) {
  $allowedposttags['pre'] = array(
    'lang' => array(),
    'line' => array(),
    'style' => array(),
    'width' => array(),
  );
  //Allow plugin use in comments
  $allowedtags['pre'] = array(
    'lang' => array(),
    'line' => array(),
  );
  $allowedtags['div'] = array(
  	'class' => array(),
  	'style' => array(),
  	'id' => array()
  );
  $allowedtags['table'] = array(
  	'class' => array(),
  	'style' => array(),
  	'id' => array()
  );
  $allowedtags['tr'] = array(
  	'class' => array(),
  	'style' => array(),
  	'id' => array()
  );
  $allowedtags['td'] = array(
  	'class' => array(),
  	'style' => array(),
  	'id' => array()
  );
  $allowedtags['tbody'] = array(
  	'class' => array(),
  	'style' => array(),
  	'id' => array()
  );
  $allowedtags['span'] = array(
  	'class' => array(),
  	'style' => array(),
  	'id' => array()
  );
}
include_once('geshi/geshi.php');

function syntax_code_trim($code)
{
    $code = preg_replace("/^\s*\n/siU", "", $code);
    $code = rtrim($code);
    return $code;
}

function syntax_substitute(&$match)
{
    global $syntax_token, $syntax_matches;

    $i = count($syntax_matches);
    $syntax_matches[$i] = $match;

    return "\n\n<p>" . $syntax_token . sprintf("%03d", $i) . "</p>\n\n";
}

function syntax_line_numbers($code, $start)
{
    $line_count = count(explode("\n", $code));
    $output = "<pre>";
    for ($i = 0; $i < $line_count; $i++)
    {
        $output .= ($start + $i) . "\n";
    }
    $output .= "</pre>";
    return $output;
}
function syntax_add_head ($output) {
	$head;
}
$id = 0;
function syntax_highlight($match)
{
    global $syntax_matches;
	global $id;
	$id += $id;
    $i = intval($match[1]);
    $match = $syntax_matches[$i];

    $language = strtolower(trim($match[1]));
    $line = trim($match[2]);
    $escaped = trim($match[3]);
    $code =  syntax_code_trim($match[6]);
    if ($escaped == "true") $code = htmlspecialchars_decode($code);

    $geshi = new GeSHi($code, $language);
    $geshi->enable_keyword_links(false);
    //do_action_ref_array('syntax_init_geshi', array(&$geshi));
    $output = '<div class="syntax">';
    
	if($match[4] != 'false'){
	    if( $match[5] && trim($match[5])!=''){
	    	$title = "$match[5]:";
	    }
	    else {
	    	$title = strtoupper($language);
	    }
		$head = "<div class=\"syntax-header\">$title<div class=\"clearfix\"></div></div>";
		$output .= $head;
    }
	$content = "\n<div class=\"syntax-content\">";
    if ($line != '0') {	
    	if($line == '') 
    		$line = '1';
        $content .= "<table><tr><td class=\"line_numbers\">";
        $content .=  syntax_line_numbers($code, $line);
        $content .= "</td><td class=\"code\" id=\"$id\">";
        $content .= $geshi->parse_code();
        $content .= "</td></tr></table>";
    }
    else
    {
        $content .= "<div class=\"code\">";
        $content .= $geshi->parse_code();
        $content .= "</div>";
    }
	$content .= "</div>";
	
    $output .= $content;
    $output .= "</div>\n";
    return $output;
}

function syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|data-head=[\"'](true|false)?[\"']|\s)+>(.*)\n(.*)<\/pre>\s*/siU",
        "syntax_substitute",
        $content
    );
}

function syntax_after_filter($content)
{
    global $syntax_token;

     $content = preg_replace_callback(
         "/<p>\s*".$syntax_token."(\d{3})\s*<\/p>/si",
         "syntax_highlight",
         $content
     );

    return $content;
}

$syntax_token = md5(uniqid(rand()));

// We want to run before other filters; hence, a priority of 0 was chosen.
// The lower the number, the higher the priority.  10 is the default and
// several formatting filters run at or around 6.
add_filter('the_content', 'syntax_before_filter', 0);
add_filter('the_excerpt', 'syntax_before_filter', 0);
add_filter('comment_text', 'syntax_before_filter', 0);

// We want to run after other filters; hence, a priority of 99.
add_filter('the_content', 'syntax_after_filter', 99);
add_filter('the_excerpt', 'syntax_after_filter', 99);
add_filter('comment_text', 'syntax_after_filter', 99);

?>
